<template>
  <div class="container">
    <div class="left">
        <div>
          <span
            v-for="(title, id) in classMappings"
            :key="id"
            @click="setActive(id)"
            :class="{ 'active-border': isActive === id }"
          >
            {{ title }}
          </span>
        </div>
    </div>
    <div class="right">
      <div class="table">
        <div class="select">
          <!-- 开始日期 -->
          <el-date-picker
            v-model="beginTime"
            type="datetime"
            placeholder="请选择开始日期"
          >
          </el-date-picker>

          <!-- 结束日期 -->
          <el-date-picker
            v-model="endTime"
            type="datetime"
            placeholder="请选择结束日期"
            style="margin: 0 20px"
          >
          </el-date-picker>
          <button @click="getPdf(isActive, beginTime, endTime)">查询</button>
        </div>
        <div class="content">
          <div class="data">
            <el-table
              height="100%"
              :data="tableData"
              style="width: 100%; margin-bottom: 20px; text-align: center"
              stripe
              :header-cell-style="headerCellStyle"
              :cell-style="cellStyle"
              :row-style="rowStyle"
            >
              <el-table-column prop="id" label="期数" width="200">
              </el-table-column>
              <el-table-column prop="fileName" label="文件名">
              </el-table-column>
              <el-table-column prop="classification" label="种类" width="150">
              </el-table-column>
              <el-table-column prop="countySign" label="发布单位" width="200">
              </el-table-column>
              <el-table-column prop="date" label="日期"> </el-table-column>
              <el-table-column prop="content" label="查看更多"
                ><template slot-scope="scope">
                  <el-button @click="getUrl(scope.row.fileName)"
                    >点击获取详情</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "forecast",
  data() {
    return {
      date: "",
      isActive: "13",
      beginTime: "",
      endTime: "",
      selectedCity: "",
      selectedCounty: "",
      selectedTownship: "",
      search: "",
      tableData: [],
      classMappings: {
        13: "未来72小时天气预报",
        23: "每旬天气预报",
        33: "每月天气预报",
        43: "气象信息快报",
        53: "重大气象信息专报",
        63: "节假日气象景观专题预报",
        73: "物候期预报",
      },
    };
  },
  methods: {
    headerCellStyle() {
      return {
        background: "#011A10",
        color: "#fff",
        opacity: 0.8,
      };
    },
    cellStyle() {
      return {
        background: "#00371D",
        color: "#fff",
      };
    },
    rowStyle() {
      return {
        height: "30px",
      };
    },
    setActive(id) {
      this.isActive = id;
      this.getPdf(id);
    },
    getPdf(classification, beginTime, endTime) {
      axios
        .get("https://816i9307t1.vicp.fun/yaqxfw/pdf/getPdf", {
          params: {
            classification: classification,
            beginTime: beginTime,
            endTime: endTime,
          },
        })
        .then((res) => {
          let short = res.data.data;
          short.forEach((item) => {
            item.classification = this.classMappings[item.classification];
          });
          this.tableData = short;
        })
        .catch((error) => {
          console.error("请求失败:", error);
        });
    },
    getUrl(fileName) {
      axios
        .get("https://816i9307t1.vicp.fun/yaqxfw/pdf/getUrl", {
          params: {
            fileName: fileName,
          },
        })
        .then((res) => {
          //console.log(res.data.data);
          window.open(res.data.data);
        })
        .catch((error) => {
          console.error("请求失败:", error);
        });
    },
  },
  mounted() {
    this.getPdf(13);
  },
};
</script>

<style lang="scss" scoped>
.active-border {
  color: #47C364 !important;
}
.container {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;

  .left {
    width: 15%;
    height: 95%;
    border: #01522c 5px dashed;
    border-radius: 20px;

    div {
      width: 100%;
      height: 98%;
      display: flex;
      justify-content: flex-start;
      flex-direction: column;
      align-items: center;
      flex-wrap: wrap;

      span {
        cursor: pointer;
        display: flex;
        justify-content: center;
        align-items: center;
        width: 95%;
        height: 10%;
        display: flex;
        color: white;
      }
    }
  }

  .right {
    width: 75%;
    height: 95%;

    /*表格 */
    .table {
      width: 100%;
      height: 100%;
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      align-items: center;

      .select {
        width: 100%;
        height: 5%;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        margin: 20px 0;
        box-sizing: b;
        button {
          width: 100px;
          height: 90%;
          background: none;
          border: #78d2a8 1px solid;
          border-radius: 5px;
          color: #fff;
        }
      }

      .time {
        width: 100%;
        height: 5%;
        display: flex;
        justify-content: flex-end;
        align-items: center;

        span {
          color: white;
          font-size: 18px;
          font-weight: bold;
        }
      }

      .content {
        width: 100%;
        height: 88%;
        display: flex;
        flex-wrap: wrap;

        .data {
          width: 100%;
          height: 100%;
        }

        .page {
          width: 100%;
          height: 10%;
        }
      }
    }
  }
}
</style>